#include "Base.h"
#include "..\Util.h"

using namespace BroodWar;
using namespace BroodWar::Terrain;

namespace BroodWar
{
	namespace Addon
	{
		Base^ ConvertBase(BWSAL::Base* base)
		{
			if(base == NULL)
				return nullptr;
			return gcnew Base(base);
		}

		BWSAL::Base* ConvertBase(Base^ base)
		{
			if(base == nullptr)
				return NULL;
			return base->instance;
		}

		Base::Base(BWSAL::Base* base)
		{
			instance = base;
		}

		Terrain::BaseLocation^ Base::BaseLocation::get()
		{
			return ConvertBaseLocation(instance->getBaseLocation());
		}
		
		Unit^ Base::ResourceDepot::get()
		{
			return ConvertUnit(instance->getResourceDepot());
		}
		
		Unit^ Base::Refinery::get()
		{
			return ConvertUnit(instance->getRefinery());
		}
		
		HashSet<Unit^>^ Base::Minerals::get()
		{
			return ToHashSet<BWAPI::Unit*, Unit^>(instance->getMinerals(), &ConvertUnit);
		}
		
		HashSet<Unit^>^ Base::Geysers::get()
		{
			return ToHashSet<BWAPI::Unit*, Unit^>(instance->getGeysers(), &ConvertUnit);
		}
		
		void Base::SetPause(bool paused)
		{
			instance->setPaused(paused);
		}
		
		bool Base::IsPaused::get()
		{
			return instance->isPaused();
		}
		
		bool Base::IsReady::get()
		{
			return instance->isReady();
		}
		
		bool Base::IsActive::get()
		{
			return instance->isActive();
		}
		
		void Base::Update()
		{
			instance->update();
		}
		
		void Base::OnUnitDestroy(Unit^ unit)
		{
			instance->onUnitDestroy(ConvertUnit(unit));
		}

		int Base::GetHashCode()
		{
			return BaseLocation->GetHashCode();
		}

		bool Base::Equals(Object^ o)
		{
			Base^ other = dynamic_cast<Base^>(o);
			return this->Equals(other);
		}

		bool Base::Equals(Base^ other)
		{
			if(ReferenceEquals(nullptr, other))
				return false;
			if(ReferenceEquals(this, other))
				return true;
			return this->BaseLocation->GetHashCode() == other->BaseLocation->GetHashCode();
		}

		bool Base::operator == (Base^ first, Base^ second)
		{
			if(ReferenceEquals(first, second))
				return true;
			if(ReferenceEquals(nullptr, first))
				return false;
			return first->Equals(second);
		}

		bool Base::operator != (Base^ first, Base^ second)
		{
			return !(first == second);
		}
	}
}
